package cn.xfdzx.android.apps.shop.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.myshop.OrderDetailPringActivity;
import cn.xfdzx.android.apps.shop.activity.remake.BigImageViewActivity;
import cn.xfdzx.android.apps.shop.bean.ShopInOrderAfterSalesListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.ShopInGoodListPresenter;

/**
 * @author gzk
 * @date 2020/5/18 0018
 * 商家入驻  订单管理的列表
 * description：
 */
public class ShopInOrderAfterSalesListAdapter extends RecyclerView.Adapter {


    private List<ShopInOrderAfterSalesListBeen.DataBean.ApplyBean> mList;
    private ItemClickListener itemClickListener;


    private Context mContext;
    private final LayoutInflater mLayoutInflater;
    private ShopInGoodListPresenter mPresenter;
    private String styleType;


    public ShopInOrderAfterSalesListAdapter(Context mContext, List<ShopInOrderAfterSalesListBeen.DataBean.ApplyBean> list, String styleType) {
        this.mContext = mContext;
        this.mList = list;

        this.styleType = styleType;

        //以后用它来初始化布局
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        return new ShopInOrderAfterSalesListAdapter.OrderViewHolder0(mContext, mLayoutInflater.inflate(R.layout.item_merchants_shop_oder_retail, null));


    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        ShopInOrderAfterSalesListAdapter.OrderViewHolder0 orderViewHolder0 = (ShopInOrderAfterSalesListAdapter.OrderViewHolder0) holder;
        orderViewHolder0.setData(position);

    }


    @Override
    public int getItemCount() {
        return mList.size();
    }


    public class OrderViewHolder0 extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_order_state)
        TextView tvOrderState;
        @BindView(R.id.tv_peisong_type)
        TextView tvPeisongType;
        @BindView(R.id.tv_address)
        TextView tvAddress;
        @BindView(R.id.tv_good_num)
        TextView tvGoodNum;
        @BindView(R.id.tv_good_pack_up)
        TextView tvGoodPackUp;
        @BindView(R.id.tv_why)
        TextView tvWhy;
        @BindView(R.id.tv_why_detail)
        TextView tvWhyDetail;
        @BindView(R.id.tv_shenhe_state)
        TextView tvShenheState;
        @BindView(R.id.tv_jindu_pack_up)
        TextView tvJinduPackUp;
        @BindView(R.id.tv_tui_money)
        TextView tvTuiMoney;
        @BindView(R.id.tv_order_num)
        TextView tvOrderNum;
        @BindView(R.id.tv_order_new_time)
        TextView tvOrderNewTime;
        @BindView(R.id.tv_order_apply_time)
        TextView tvOrderApplyTime;
        @BindView(R.id.rv_goods)
        RecyclerView rvGoods;
        @BindView(R.id.rv_why_image)
        RecyclerView rvWhyImage;
        @BindView(R.id.rv_progress)
        RecyclerView rvProgress;


        private Context mContext;

        public OrderViewHolder0(Context mContext, View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            this.mContext = mContext;

        }

        public void setData(int position) {
            tvName.setText(mList.get(position).getUser_name());

            if (mList.get(position).getStatus() == 1) {
                tvOrderState.setText("等待平台审核");
            } else if (mList.get(position).getStatus() == 2) {
                tvOrderState.setText("平台审核通过");
            } else if (mList.get(position).getStatus() == 3) {
                tvOrderState.setText("平台审核驳回");
            } else if (mList.get(position).getStatus() == 4) {
                tvOrderState.setText("取消申请");
            }  else if (mList.get(position).getStatus() == 5) {
                tvOrderState.setText("退款中");
            } else if (mList.get(position).getStatus() == 6) {
                tvOrderState.setText("退款成功");
            }
            tvPeisongType.setText(mList.get(position).getDelivery_name());
            if (null!=mList.get(position).getAddress()) {
                tvAddress.setText(mList.get(position).getAddress().trim());
            }
            tvGoodNum.setText(mList.get(position).getApply_goods_count() + "件退款商品");


            tvWhy.setText("退款原因：" + mList.get(position).getReason());
            tvWhyDetail.setText("问题描述：" + mList.get(position).getDesc());

            if (mList.get(position).getProgress().size() != 0) {
                String time = mList.get(position).getProgress().get(0).getTime();
                if (null!=time) {
                    tvShenheState.setText(time.substring(time.length() - 8, time.length()) + "  " + mList.get(position).getProgress().get(0).getMsg());
                }
            }
            tvTuiMoney.setText("¥" + mList.get(position).getApply_price());
            tvOrderNum.setText("订单号：" + mList.get(position).getOrder_id());
            tvOrderNewTime.setText("下单时间：" + mList.get(position).getOrder_created_at());
            tvOrderApplyTime.setText("申请时间：" + mList.get(position).getCreated_at());

            // 商品名称列表
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
            linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            rvGoods.setLayoutManager(linearLayoutManager);
            rvGoods.setItemAnimator(new DefaultItemAnimator());

            ShopInOrderAfterSalesGoodListAdapter goodListAdapter = new ShopInOrderAfterSalesGoodListAdapter(mContext, mList.get(position).getApplyCart());

            rvGoods.setAdapter(goodListAdapter);

            if (!mList.get(position).isGood_up_open()) {
                tvGoodPackUp.setText("展开");
                Drawable nav_up = mContext.getResources().getDrawable(R.drawable.icon_after_sales_hui_xia);
                nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                tvGoodPackUp.setCompoundDrawables(null, null, nav_up, null);
            } else {
                tvGoodPackUp.setText("收起");
                Drawable nav_up = mContext.getResources().getDrawable(R.drawable.icon_after_sales_hui_shang);
                nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                tvGoodPackUp.setCompoundDrawables(null, null, nav_up, null);
            }

            tvGoodPackUp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mList.get(position).isGood_up_open()) {
                        mList.get(position).setGood_up_open(false);
                        goodListAdapter.setSize(false);
                        tvGoodPackUp.setText("展开");
                        Drawable nav_up = mContext.getResources().getDrawable(R.drawable.icon_after_sales_hui_xia);
                        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                        tvGoodPackUp.setCompoundDrawables(null, null, nav_up, null);

                    } else {
                        mList.get(position).setGood_up_open(true);
                        goodListAdapter.setSize(true);
                        tvGoodPackUp.setText("收起");
                        Drawable nav_up = mContext.getResources().getDrawable(R.drawable.icon_after_sales_hui_shang);
                        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                        tvGoodPackUp.setCompoundDrawables(null, null, nav_up, null);
                    }

                }
            });
// 商品图
            LinearLayoutManager manager = new LinearLayoutManager(mContext);
            manager.setOrientation(LinearLayoutManager.HORIZONTAL);
            rvWhyImage.setLayoutManager(manager);
            ShopInOrderAfterSalesImageAdapter shopInOrderAfterSalesImageAdapter = new ShopInOrderAfterSalesImageAdapter(mContext, mList.get(position).getImgs());
            rvWhyImage.setAdapter(shopInOrderAfterSalesImageAdapter);
            shopInOrderAfterSalesImageAdapter.setOnItemClickListener(new ItemClickListener() {
                @Override
                public void onItemClick(int i) {
                    Intent intent = new Intent(mContext, BigImageViewActivity.class);
                    intent.putStringArrayListExtra("imgs",(ArrayList<String>)mList.get(position).getImgs());
                    intent.putExtra("position",i);
                    mContext.startActivity(intent);
                }
            });

//进度
            LinearLayoutManager jindu = new LinearLayoutManager(mContext);
            jindu.setOrientation(LinearLayoutManager.VERTICAL);
            rvProgress.setLayoutManager(jindu);
            rvProgress.setNestedScrollingEnabled(false);
            ShopInOrderAfterSalesProgressListAdapter shopInOrderAfterSalesProgressListAdapter = new ShopInOrderAfterSalesProgressListAdapter(mContext, mList.get(position).getProgress());
            rvProgress.setAdapter(shopInOrderAfterSalesProgressListAdapter);


            if (!mList.get(position).isProgress_up_open()) {
                tvJinduPackUp.setText("展开进度");
                Drawable nav_up = mContext.getResources().getDrawable(R.drawable.icon_after_sales_blue_xia);
                nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                tvJinduPackUp.setCompoundDrawables(null, null, nav_up, null);
                rvProgress.setVisibility(View.GONE);
            } else {
                tvJinduPackUp.setText("收起进度");
                Drawable nav_up = mContext.getResources().getDrawable(R.drawable.icon_after_sales_blue_shang);
                nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
                tvJinduPackUp.setCompoundDrawables(null, null, nav_up, null);
                rvProgress.setVisibility(View.VISIBLE);
            }
            tvJinduPackUp.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mList.get(position).isProgress_up_open()) {
                        mList.get(position).setProgress_up_open(false);
//                        tvJinduPackUp.setText("展开进度");
//                        Drawable nav_up = mContext.getResources().getDrawable(R.drawable.icon_after_sales_blue_xia);
//                        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
//                        tvJinduPackUp.setCompoundDrawables(null, null, nav_up, null);
                    } else {
                        mList.get(position).setProgress_up_open(true);
//                        tvJinduPackUp.setText("收起进度");
//                        Drawable nav_up = mContext.getResources().getDrawable(R.drawable.icon_after_sales_blue_shang);
//                        nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
//                        tvJinduPackUp.setCompoundDrawables(null, null, nav_up, null);
                    }
                    notifyDataSetChanged();
                }
            });

        }
    }

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }
}
